Run census api key to pull (NOTE: only need to do this once).

In order to obtain a tidycensus key, visit http://api.census.gov/data/key_signup.html

# census_api_key("INSERT CODE HERE",
#                overwrite=T, install=T)

Specify Needed Years and Codes

Possible values for geo: county (“county”), zip code (“zcta”), many more found at https://walker-data.com/tidycensus/articles/basic-usage.html

As a general note, zcta maps take much longer as shapefiles are much more detailed than county (~500mb v ~75mb).

needed_years <- seq(2011, 2013, by = 1)
geo <- "zcta"

Pull SES data

Possible years are dependent on geographical input: For zcta (2011 - 2021), for county (2009 - 2021).

After running this line, variables removed due to incompleteness for any of the years (ie. variables not taken during year x) are listed.

This code chunk may take a few minutes, dependent on how many years of data are being pulled.

# get codes from excel file found in codes_data folder
my_codes <- get_existing_codes(years = needed_years, file_path = paste0(here::here(), '/codes_data/acs_2014_codes_updated.csv')) 
## [1] "Removed vars: broadband_total"      "Removed vars: broadband_any"       
## [3] "Removed vars: broadband_comp_total" "Removed vars: broadband_comp"      
## [5] "Removed vars: broadband_comp_dial"
# pull data
dat_all <- get_multiple_ACS(years = needed_years, codes = my_codes, geo = geo)
## Getting data from the 2007-2011 5-year ACS
## Joining, by = "state_code"
## Getting data from the 2008-2012 5-year ACS
## Joining, by = "state_code"
## Getting data from the 2009-2013 5-year ACS
## Joining, by = "state_code"
dat_all
## # A tibble: 99,360 × 21
##     zcta GEOID   StateAb year  populat…¹ male_…² white…³ pop_n…⁴ hispa…⁵ age_u…⁶
##    <dbl> <chr>   <chr>   <chr>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 35004 0135004 AL      2011       9766   0.511   0.884 9.16e-2  0.0352  0.0498
##  2 35005 0135005 AL      2011       8411   0.509   0.633 2.99e-1  0.0769  0.0380
##  3 35006 0135006 AL      2011       3104   0.534   0.951 3.90e-2  0       0.0552
##  4 35007 0135007 AL      2011      25954   0.477   0.780 1.43e-1  0.0932  0.0443
##  5 35010 0135010 AL      2011      21289   0.478   0.688 2.87e-1  0.0403  0.0396
##  6 35013 0135013 AL      2011         42   0.619   0.762 0        0.857   0.0345
##  7 35014 0135014 AL      2011       5151   0.689   0.401 5.46e-1  0.0538  0.0120
##  8 35016 0135016 AL      2011      16628   0.496   0.962 7.82e-4  0.0110  0.0305
##  9 35019 0135019 AL      2011       2069   0.538   0.974 9.67e-4  0.0773  0.0731
## 10 35020 0135020 AL      2011      26898   0.434   0.192 7.83e-1  0.0406  0.0441
## # … with 99,350 more rows, 11 more variables: age_5_17_prop <dbl>,
## #   age_18_39_prop <dbl>, age_40_64_prop <dbl>, age_over_65_prop <dbl>,
## #   poverty_prop <dbl>, family_type_female_householder_prop <dbl>,
## #   family_type_single_prop <dbl>, housing_rental_prop <dbl>,
## #   crowded_prop <dbl>, phone_service_prop <dbl>, public_assistance_prop <dbl>,
## #   and abbreviated variable names ¹​population, ²​male_prop, ³​white_prop,
## #   ⁴​pop_non_hispanic_black_prop, ⁵​hispanic_prop, ⁶​age_under_5_prop
## # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names

Mapping

Run the below line to download and clean shape plots (NOTE: only need to do this once).

This may take a few minutes.

# source(paste0(here::here(), '/shape_pull/shape_pull.R'))

Note plotting can only be done with geographies “zcta”, or “county”.

Below pulls in appropriate shape plot. You should include this in all scripts.

if (geo == "zcta"){
  shape <- readOGR(here::here('shape_pull/zcta_shape'), layer = 'zcta')
}
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/nickbachelder/Desktop/Jefferson 2023/SES Functions/shape_pull/zcta_shape", layer: "zcta"
## with 33144 features
## It has 10 fields
if (geo == "county"){
  shape <- readOGR(here::here('shape_pull/county_shape'), layer = 'county')
}

Leaflet State Plot Example

# Also Try: States = "All'
# You can drag below
get_leaflet(dat_all = dat_all, states = 'NC', years ="2011", attribute = 'age_over_65_prop', geo = geo)

State ggplot Example

get_geom_poly(dat_all = dat_all, states = 'PA', years = '2013', attribute = 'age_over_65_prop', geo = geo)

State ggplot Progression Example

see_year_progression(dat_all = dat_all, states = 'PA', attribute = 'age_over_65_prop', geo = geo, years = needed_years)

Adding New Variables

Run below line if you want to see all possible census variables (1045 topics, some seperated by age, etc). Previously used variables are found and used in the data_codes folder, but more can be added (Note: if you would like to do this, more cleaning may be neccecary in the pull function)

# load_variables(2015, "acs5", cache = TRUE)$concept %>% unique()